---
title: "Table 3"
author: "Yingjie Fan"
date: "2023-06-02"
---

```{r echo=FALSE, message=FALSE, warning=FALSE}
rm(list=ls())
#install.packages(c("dplyr", "tidyr", "zoo", "data.table", "nlme"))
#Load Packages
library(dplyr)
library(tidyr)
library(zoo)
library(data.table)
library(nlme)
path = "" # Insert path
```

```{r Table S1: Number of Tweets Analyzed}
follower_d = fread(paste0(path,"/Data/Followers/follower.csv"))%>%
  mutate(followers = 1000000*followers)%>%
  group_by(username)%>%
  summarise(date = min(as.Date(date)))
#The number of tweets per account
vol_total_summary = fread(paste0(path,"/Data/Tweets/vol_total_summary.csv"), drop = TRUE) %>%
  group_by(username) %>%
  left_join(follower_d)

write.csv(vol_total_summary,file=paste0(path,"/Output/tableS1_tweet_volume_total.csv"),fileEncoding = "UTF-8")
```

```{r Table S3: Model Performance}
narrative_performance <-fread(paste0(path,'/Data/Model Performance/narratives.csv'),drop = T)%>%
  group_by(username)%>%
  filter(f1 == max(f1)) %>% slice_max(order_by = f1)%>%
  mutate(category = "narratives")

softnews_performance = fread(paste0(path,'/Data/Model Performance/softnews.csv'),drop = T)%>%
  group_by(username)%>%
  filter(f1 == max(f1)) %>% slice_max(order_by = f1)%>%
  mutate(category = "softnews")

model_performance = rbind(softnews_performance,narrative_performance)
write.csv(model_performance,file=paste0(path,"/Output/tableS3_model_performance.csv"),fileEncoding = "UTF-8")
```


```{r Table S4: ITSA for Follower Growth, results='asis'}
#Data source information can be found in Figure 1.Rmd
follower_d = fread(paste0(path,"/Data/Followers/follower.csv"))%>%
  mutate(followers = 1000000*followers)%>%
  mutate(date = as.Date(date))%>%
  filter(username %in% c('BBC Breaking','CGTN',"RT","CNN Breaking","Xinhua"))%>%
  arrange(date)%>%
    group_by(username) %>%
    mutate(volume=tweets-lag(tweets))%>%
  mutate(f_growth = followers-lag(followers))%>%
  ungroup()%>%
  mutate(D=ifelse(date>"2017-07-20",1,0)) %>%
  mutate(T=as.numeric(date)-as.numeric(as.Date("2017-07-01")))%>%
  filter(!between(date, as.Date("2017-05-01"), as.Date("2017-09-01")))

follower_irts_result<-lapply(split(follower_d, factor(follower_d$username)), function(x)gls(f_growth ~ T + D + T*D, data=x, correlation = corAR1(form = ~ 1),na.action = na.omit))

summary(follower_irts_result$CGTN)
summary(follower_irts_result$Xinhua)
summary(follower_irts_result$RT)
summary(follower_irts_result$`CNN Breaking`)
summary(follower_irts_result$`BBC Breaking`)
```

```{r load data for Table S5 and S6}

content_d<-fread(paste0(path,'/Data/Classification/output_summary.csv'))%>%
  mutate(timestamp=as.Date(timestamp,"%Y-%m-%d"))%>%
  mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
  mutate(D=ifelse(timestamp>"2017-07-20",1,0))%>%
  mutate(T=as.numeric(timestamp)-as.numeric(as.Date("2017-07-01")))%>%
  mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
  filter(!between(timestamp, as.Date("2017-05-01"), as.Date("2017-09-01")))

```

```{r Table S5: ITSA for Narratives, results='asis'}
narrative_irts_result<-lapply(split(content_d, factor(content_d$username)), function(x)gls(100*mean_narrative ~ T + D + T*D, data=x, correlation = corAR1(form = ~ 1),na.action = na.omit))

summary(narrative_irts_result$CGTN)
summary(narrative_irts_result$Xinhua)
summary(narrative_irts_result$`People's\ Daily`)
summary(narrative_irts_result$`China Daily`)
```

```{r Table S6: ITSA for Soft News, results='asis'}
softnews_irts_result<-lapply(split(content_d, factor(content_d$username)), function(x)gls(100*mean_softnews ~ T + D + T*D, data=x, correlation = corAR1(form = ~ 1),na.action = na.omit))

summary(softnews_irts_result$CGTN)
summary(softnews_irts_result$Xinhua)
summary(softnews_irts_result$`People's\ Daily`)
summary(softnews_irts_result$`China Daily`)

```

```{r Table S7: ITSA for Country Coverage, results='asis'}
country = fread(paste0(path,"/Data/Country Mapping/geo_analysis_output.csv"))%>%
  mutate(month=as.yearmon(month))%>%
  filter(month<as.yearmon("Jan 2020"))%>%
  filter(month>as.yearmon("Dec 2015"))%>%
  mutate(D=ifelse(month>as.yearmon("Jul 2017"),1,0)) %>%
  mutate(T=(as.numeric(month)-as.numeric(as.yearmon("2017-07-01")))*12)%>%
  filter(!between(month, as.yearmon("2017-05-01"), as.yearmon("2017-08-31")))

country_irts_result<-lapply(split(country, factor(country$username)), function(x)gls(country_count ~ T + D + T*D, data=x, correlation = corAR1(form = ~ 1),na.action = na.omit))

summary(country_irts_result$CGTN)
summary(country_irts_result$Xinhua)
summary(country_irts_result$`People's Daily`)
summary(country_irts_result$`China Daily`)
summary(country_irts_result$`Al Jazeera`)
summary(country_irts_result$`BBC Breaking`)
summary(country_irts_result$`CNN Breaking`)
summary(country_irts_result$RT)

```

```{r Table S8: ITSA for Sentiment, results='asis'}
sent_all = fread(paste0(path,"/Data/Sentiment/sentiment_summary.csv"))%>%
  mutate(timestamp=as.Date(timestamp))%>%
  mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
    filter(timestamp<"2020-01-23")%>%
  filter(timestamp>"2013-01-23")%>%
  group_by(username, timestamp)%>%
  dplyr::summarize(sent_mean=mean(compound))%>%
  mutate(D=ifelse(as.Date(timestamp)>"2017-07-20",1,0)) %>%
  mutate(T=as.numeric(timestamp)-as.numeric(as.Date("2017-07-01")))%>%
  mutate(month=as.yearmon(substr(timestamp,1,7)))%>%
  filter(!between(timestamp, as.Date("2017-05-01"), as.Date("2017-09-01")))

sent_all_irts<-lapply(split(sent_all, factor(sent_all$username)), function(x)gls(sent_mean ~ T + D + T*D, data=x, correlation = corAR1(form = ~ 1),na.action = na.omit))

summary(sent_all_irts$CGTN)
summary(sent_all_irts$Xinhua)
summary(sent_all_irts$`People's Daily`)
summary(sent_all_irts$`China Daily`)
summary(sent_all_irts$`Al Jazeera`)
summary(sent_all_irts$`BBC Breaking`)
summary(sent_all_irts$`CNN Breaking`)
summary(sent_all_irts$RT)


```

